package com.hm.service.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;


@Service
public class OpencvService {
    private static final Logger logger = LoggerFactory.getLogger(OpencvService.class);

    public void initOpenCV() {
        String osName = System.getProperty("os.name").toLowerCase();
        logger.warn("该系统是：{}", osName);
        if (osName.contains("win")) {
            initOpenCVToWin();
        } else if (osName.contains("nux")) {
            initOpenCVToLinux();
        } else {
            logger.warn("不支持该系统！");
        }
    }

    private void initOpenCVToWin() {
        try (InputStream libraryStream = getClass().getResourceAsStream("/open-cv-x64" + "/opencv_java4100.dll")) {
            if (libraryStream == null) {
                return;
            }
            //1、创建文件夹
            File folder = new File("C:/SupportLibrary");
            if (!folder.exists()) {
                boolean isMkdirFolder = folder.mkdirs();
                logger.warn("文件夹创建状态：{}", isMkdirFolder);
            }
            //2、创建空白文件
            File tempFile = new File(folder, "opencv-64.dll");
            if (tempFile.exists()) { // 文件存在
                boolean delete = tempFile.delete();
                logger.warn("文件删除状态：{}", delete);
            }
            boolean newFile = tempFile.createNewFile();
            logger.warn("空白文件创建状态：{}", newFile);
            //3、把流写入空白文件
            try (OutputStream outputStream = new FileOutputStream(tempFile)) {
                byte[] buffer = new byte[1024];
                int length;
                while ((length = libraryStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, length);
                }
            }
            // 加载临时文件
            String absolutePath = tempFile.getAbsolutePath();
            System.load(absolutePath);
            logger.warn("opencv加载成功-路径：{}", absolutePath);
        } catch (Exception e) {
            logger.warn("opencv加载失败：{}", e.getMessage());
        }
    }

    private void initOpenCVToLinux() {
        try (InputStream libraryStream = getClass().getResourceAsStream("/open-cv-x64" + "/libopencv_java4100.so")) {
            if (libraryStream == null) {
                return;
            }
            //1、创建文件夹
            File folder = new File("/support-library");
            if (!folder.exists()) {
                boolean isMkdirFolder = folder.mkdirs();
                logger.warn("文件夹创建状态：{}", isMkdirFolder);
            }
            //2、创建空白文件
            File tempFile = new File(folder, "opencv-64.so");
            if (tempFile.exists()) { // 文件存在
                boolean delete = tempFile.delete();
                logger.warn("文件删除状态：{}", delete);
            }
            boolean newFile = tempFile.createNewFile();
            logger.warn("空白文件创建状态：{}", newFile);
            //3、把流写入空白文件
            try (OutputStream outputStream = new FileOutputStream(tempFile)) {
                byte[] buffer = new byte[1024];
                int length;
                while ((length = libraryStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, length);
                }
            }
            // 加载临时文件
            String absolutePath = tempFile.getAbsolutePath();
            System.load(absolutePath);
            logger.warn("opencv加载成功-路径：{}", absolutePath);
        } catch (Exception e) {
            logger.warn("opencv加载失败：{}", e.getMessage());
        }
    }


}
